﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Gin.Transactions;
using System.Windows.Forms;

namespace Gin
{
    public class ExecutionContext
    {

        public const string TEMP_SUBFOLDER_NAME = @"temp";
        public const string PACKAGES_SUBFOLDER_NAME = @"packages";

        private Dictionary<string, object> _results;
        private Dictionary<string, object> _inputs;

        public string GinPath { get; private set; }
        public string PackagesPath { get; private set; }
        public string TempPath { get; private set; }

        public Control ControlContainer { get; set; }
        public Package ExecutedPackage { get; set; }

        public ExecutionContext()
        {
            _results = new Dictionary<string, object>();
            _inputs = new Dictionary<string, object>();
            InitGeneralPaths();
        }

        private void InitGeneralPaths()
        {
            GinPath = AppSettings.Default.RootPath;
            PackagesPath = Path.Combine(GinPath, PACKAGES_SUBFOLDER_NAME);
            TempPath = Path.Combine(GinPath, TEMP_SUBFOLDER_NAME);
        }

        public void SaveResult(string key, object value)
        {
            _results[key] = value;
        }

        public object GetResult(string key)
        {
            return _results[key];
        }


        public string GetAbsolutePathFrom(string path)
        {
            foreach (var input in _inputs)
            {
                path = path.Replace(input.Key, input.Value.ToString());
                path = path.Replace("\\\\", "\\");
            }
            return path;
        }

        public void SaveUserInput(string key, object value)
        {
            if (!key.StartsWith("%"))
            {
                key = "%" + key;
            }
            if (!key.EndsWith("%"))
            {
                key = key + "%";
            }

            _inputs[key] = value;
        }
    }
}
